package com.tcsl.mylibrary.ums;

import android.app.Activity;

import com.ums.AppHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * 描述: 全民惠业务的工具类
 * <br/>作者：吴永弘
 * <br/>创建时间: 2016/11/28 14:58
 */
public class UmsBusinessUtils {
    private static final void doBusiness(Activity activity, String businessType, Map<String, String> map) {
        JSONObject json = new JSONObject();
        //如果有字段参数
        if (map != null) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                try {
                    json.put(key, map.get(key));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        AppHelper.callTrans(activity, UmsBusinessCategory.QMH.toString(), businessType, json);
    }

    /**
     * 消费
     *
     * @param activity 当前activity
     * @param amt      金额（分）
     */
    public static final void qmhConsume(Activity activity, int amt) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        doBusiness(activity, QmhBusinessType.CONSUME.toString(), map);
    }

    /**
     * 消费撤销
     *
     * @param activity 当前activity
     * @param traceNo  原交易凭证号(6位)
     */
    public static final void qmhCancelConsumption(Activity activity, String traceNo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("orgTraceNo", traceNo);
        doBusiness(activity, QmhBusinessType.CANCEL_CONSUMPTION.toString(), map);
    }

    /**
     * 查询余额
     *
     * @param activity 当前activity
     */
    public static final void qmhQueryBalance(Activity activity) {
        doBusiness(activity, QmhBusinessType.QUERY_BALANCE, null);
    }

    /**
     * 结算
     *
     * @param activity
     */
    public static final void qmhSettle(Activity activity) {
        doBusiness(activity, QmhBusinessType.SETTLEMENT, null);
    }

    /**
     * 交易明细（重打印）
     *
     * @param activity
     */
    public static final void qmhtransactionDetail(Activity activity, String traceNo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("traceNo", traceNo);
        doBusiness(activity, QmhBusinessType.TRANSACTION_DETAIL, map);
    }

    /**
     * 预授权
     *
     * @param activity
     * @param amt      预授权金额
     */
    public static final void qmhPreAuth(Activity activity, int amt) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        doBusiness(activity, QmhBusinessType.PRE_AUTH, map);
    }

    /**
     * 预授权撤销
     *
     * @param activity
     * @param useScan       是否快速预授权撤销
     * @param amt           交易金额
     * @param authNo        交易金额
     * @param date          日期（MMDD）
     * @param cardNo        原预授权卡号（非必选字段，该字段有值时默认为通过手输卡号方式进行预授权撤销）
     * @param cardValidDate 原预授权卡有效期（当cardNo不为空时，该字段必传，若卡片不存在有效期，传0000）
     */
    public static final void qmhCancelPreAuth(Activity activity, boolean useScan, int amt, String authNo, String date, String cardNo, String cardValidDate) {
        Map<String, String> map = new HashMap<String, String>();
        if (useScan) {
            map.put("useScan", String.valueOf(useScan));
        } else {
            map.put("amt", String.valueOf(amt));
            map.put("authNo", authNo);
            map.put("date", date);
            map.put("cardNo", cardNo);
            map.put("cardValidDate", cardValidDate);
        }
        doBusiness(activity, QmhBusinessType.CANCEL_PRE_AUTH, map);
    }

    /**
     * 预授权完成
     *
     * @param activity
     */
    public static final void qmhCompletePreAuth(Activity activity) {
        doBusiness(activity, QmhBusinessType.COMPLETE_PRE_AUTH, null);
    }

    /**
     * 预授权完成(请求)撤销
     *
     * @param activity
     * @param traceNo
     */
    public static final void qmhCancelPreAuthCompletion(Activity activity, String traceNo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("traceNo", traceNo);
        doBusiness(activity, QmhBusinessType.CANCEL_PRE_AUTH_COMPLETION, map);
    }

    /**
     * 券码验证
     *
     * @param activity
     * @param ticketNo 券号
     */
    public static final void qmhCheckTicketNo(Activity activity, String ticketNo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("ticketNo", ticketNo);
        doBusiness(activity, QmhBusinessType.CHECK_TICKET_NO, map);
    }

    /**
     * 权益验证
     *
     * @param activity
     * @param cardNo
     */
    public static final void qmhCheckRight(Activity activity, String cardNo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("cardNo", cardNo);
        doBusiness(activity, QmhBusinessType.CHECK_RIGHT, map);
    }

    /**
     * 积分消费
     *
     * @param activity
     * @param amt
     */
    public static final void qmhConsumeBonus(Activity activity, int amt) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        doBusiness(activity, QmhBusinessType.CONSUME_BONUS, map);
    }

    /**
     * 非全单消费
     *
     * @param activity
     * @param amt      交易金额
     * @param discount 可打折金额
     */
    public static final void qmhConsumeNotFully(Activity activity, int amt, int discount) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        map.put("half_amt", String.valueOf(discount));
        doBusiness(activity, QmhBusinessType.CONSUME_NOT_FULLY, map);
    }

    /**
     * 退货
     *
     * @param activity
     * @param amt      交易金额
     * @param refNo    参考号
     * @param date     日期（MMDD）
     */
    public static final void qmhRecall(Activity activity, int amt, String refNo, String date) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        map.put("refNo", refNo);
        map.put("date", date);
        doBusiness(activity, QmhBusinessType.RECALL, map);
    }

    /**
     * 钱包优惠券
     *
     * @param activity
     * @param amt
     */
    public static final void qmhWalletTicket(Activity activity, int amt) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        doBusiness(activity, QmhBusinessType.WALLET_TICKET, map);
    }

    /**
     * 钱包积分
     *
     * @param activity
     * @param amt
     */
    public static final void qmhWalletBonus(Activity activity, int amt) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        doBusiness(activity, QmhBusinessType.WALLET_BONUS, map);
    }

    // TODO: 2016/11/28 文档中缺少入参JSON????????
    public static final void qmhWalletElectronicTicket(Activity activity, int amt) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        doBusiness(activity, QmhBusinessType.WALLET_ELECTRONIC_TICKET, map);
    }

    /**
     * 钱包撤销
     *
     * @param activity
     * @param orgTraceNo 原交易凭证号
     */
    public static final void qmhCancelWallet(Activity activity, String orgTraceNo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("orgTraceNo", String.valueOf(orgTraceNo));
        doBusiness(activity, QmhBusinessType.CANCEL_WALLET, map);
    }

    /**
     * @param activity
     * @param amt      交易金额
     * @param refNo    参考号
     * @param date     日期（MMDD）
     */
    public static final void qmhRecallWallet(Activity activity, int amt, String refNo, String date) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("amt", String.valueOf(amt));
        map.put("refNo", refNo);
        map.put("date", date);
        doBusiness(activity, QmhBusinessType.RECALL_WALLET, map);
    }

    /**
     * 打印交易汇总
     *
     * @param activity
     */
    public static final void qmhPrintTransactionCollection(Activity activity) {
        doBusiness(activity, QmhBusinessType.PRINT_TRANSACTION_COLLECTION, null);
    }
}
